﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Product_AddProduct : System.Web.UI.Page
{
    public int ProdID
    {
        get { return Convert.ToInt32(ViewState["_ProdID"]); }
        set { ViewState["_ProdID"] = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ProdID = DongDong.Common.Misc.GetQueryStringAsInt("PID");
            GetDrpDownValue();


            if (ProdID != 0)
            {
                GetProInfo();
            }
            else
            {
                lblCPNH.Text = DongDong.Common.Misc.ConvertToShortDateTime(System.DateTime.Now, "yyyyMMddmmss");
            }

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Product objProduct = new Product();
        //if (ProdID != 0)
        //{
        //    objProduct = Product.Get(ProdID);
        //}
        //objProduct.ProductID = lblCPNH.Text;
        //objProduct.RootID = DongDong.Common.Misc.ConvertToInt(drpZhuFenLei.SelectedValue);
        //objProduct.ParentID = DongDong.Common.Misc.ConvertToInt(drpCiFenLei.SelectedValue);
        //objProduct.NavaID = DongDong.Common.Misc.ConvertToInt(drpFenLei.SelectedValue);
        //objProduct.Title = txtTitle.Text.Trim();
        //objProduct.Price = DongDong.Common.Misc.ConvertToDouble(DongDong.Common.Misc.ConvertToDouble(txtJiaGe.Text.Trim()).ToString("f2"));
        //objProduct.JiFen = DongDong.Common.Misc.ConvertToDouble(txtJiFen.Text.Trim());
        //objProduct.PubPrice = DongDong.Common.Misc.ConvertToDouble(txtShiChangJia.Text.Trim());
        //objProduct.BrandID = DongDong.Common.Misc.ConvertToInt(drpPinPai.SelectedValue);
        //objProduct.Num = DongDong.Common.Misc.ConvertToInt(txtCount.Text.Trim());
        //objProduct.Des = txtJieShao.Text;
        //objProduct.PayType = DongDong.Common.Misc.ConvertToInt(drpPayType.SelectedValue);
        //objProduct.Level = DongDong.Common.Misc.ConvertToInt(drpLevel.SelectedValue);

        //objProduct.Rank = System.DateTime.Now;
        //if (ProdID != 0)
        //{
        //    objProduct.Update();
        //    litMessage.Text = "修改成功";
        //}
        //else
        //{
        //    litMessage.Text = "添加成功";
        //    objProduct.Add();
        //    ProdID = objProduct.ID;
        //}
    }

    public void GetProInfo()
    {
        //Product objPro = Product.Get(ProdID);
        //if (objPro != null)
        //{
        //    lblCPNH.Text = objPro.ProductID;
        //    try
        //    {
        //        drpZhuFenLei.SelectedValue = objPro.RootID.ToString();

        //        drpCiFenLei.DataSource = Nava.GetAllByParentID_TypeID(objPro.RootID, (int)Nava.type.购物);
        //        drpCiFenLei.DataValueField = "ID";
        //        drpCiFenLei.DataTextField = "Title";
        //        drpCiFenLei.DataBind();
        //        drpCiFenLei.Items.Insert(0, new ListItem("请选择", "0"));
        //        drpCiFenLei.SelectedValue = objPro.ParentID.ToString();

        //        drpFenLei.DataSource = Nava.GetAllByParentID_TypeID(objPro.ParentID, (int)Nava.type.购物);
        //        drpFenLei.DataValueField = "ID";
        //        drpFenLei.DataTextField = "Title";
        //        drpFenLei.DataBind();
        //        drpFenLei.Items.Insert(0, new ListItem("请选择", "0"));
        //        drpFenLei.SelectedValue = objPro.NavaID.ToString();

        //    }
        //    catch
        //    {


        //    }
        //    drpPinPai.SelectedValue = objPro.BrandID.ToString();

        //    txtTitle.Text = objPro.Title;
        //    txtJiaGe.Text = objPro.Price.ToString();
        //    txtShiChangJia.Text = objPro.PubPrice.ToString();
        //    txtJiFen.Text = objPro.JiFen.ToString();
        //    txtCount.Text = objPro.Num.ToString();
        //    txtJieShao.Text = objPro.Des ?? "";
        //    drpLevel.SelectedValue = objPro.Level.ToString();
        //    drpPayType.SelectedValue = objPro.PayType.ToString();

        //    rptTuPian.DataSource = Img.GetAllByForeignID_TypeID(ProdID, (int)Img.ImageType.电子产品);
        //    rptTuPian.DataBind();
        //}
    }

    public void GetDrpDownValue()
    {
        //drpZhuFenLei.DataSource = Nava.GetAllByParentID_TypeID(0, (int)Nava.type.购物);
        //drpZhuFenLei.DataValueField = "ID";
        //drpZhuFenLei.DataTextField = "Title";
        //drpZhuFenLei.DataBind();
        drpZhuFenLei.Items.Insert(0, new ListItem("请选择", "0"));
        drpCiFenLei.Items.Insert(0, new ListItem("请选择", "0"));
        drpFenLei.Items.Insert(0, new ListItem("请选择", "0"));



        //drpPinPai.DataSource = Brand.GetAll();
        //drpPinPai.DataValueField = "ID";
        //drpPinPai.DataTextField = "Title";
        //drpPinPai.DataBind();
        drpPinPai.Items.Insert(0, new ListItem("请选择", "0"));


        //drpLevel.DataSource = JiFenLevel.GetAllByType((int)JiFenLevel.Leveltype.购物).ToList();
        //drpLevel.DataValueField = "ID";
        //drpLevel.DataTextField = "LevelName";
        //drpLevel.DataBind();


        //drpPayType.PopulateFromEnum<Product.ProPayType>();

    }
    protected void drpZhuFenLei_TextChanged(object sender, EventArgs e)
    {
        int ParentID = DongDong.Common.Misc.ConvertToInt(drpZhuFenLei.SelectedValue);
        drpCiFenLei.DataSource = Nava.GetAllByParentID(ParentID);
        drpCiFenLei.DataValueField = "ID";
        drpCiFenLei.DataTextField = "Title";
        drpCiFenLei.DataBind();
        drpCiFenLei.Items.Insert(0, new ListItem("请选择", "0"));
    }
    protected void drpCiFenLei_TextChanged(object sender, EventArgs e)
    {
        int ParentID = DongDong.Common.Misc.ConvertToInt(drpCiFenLei.SelectedValue);
        drpFenLei.DataSource = Nava.GetAllByParentID(ParentID);
        drpFenLei.DataValueField = "ID";
        drpFenLei.DataTextField = "Title";
        drpFenLei.DataBind();
        drpFenLei.Items.Insert(0, new ListItem("请选择", "0"));
    }
    protected void btnTiJiao_Click(object sender, EventArgs e)
    {
        Client ObjClient = new Client();
        //是否是更新
        if (ProdID != 0)
        {
            ObjClient = Client.Get(ProdID);

        }
        ObjClient.Username = txtCount.Text;


        if (ProdID != 0)
        { 
            //进行更新
            ObjClient.Update();
            //更新提示信息
        }
        else
        {
            //进行添加
            ObjClient.Add();
            //添加提示信息

        }


        Response.Redirect("AddProduct.aspx?ID=" + ProdID);
    }


    protected void rptTuPian_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "Delete")
        {
            int ID = DongDong.Common.Misc.ConvertToInt(e.CommandArgument);
            Img.Delete(ID);
            GetProInfo();
        }
    }
}
